package com.cloudspace.jindun.network.api;

import android.text.TextUtils;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.model.ContactPersonListResult;
import com.cloudspace.jindun.model.OrganizationListResult;
import com.cloudspace.jindun.model.RemarkResult;
import com.cloudspace.jindun.network.HttpManager;
import com.cloudspace.jindun.network.base.ApiCallback;
import com.cloudspace.jindun.network.base.GsonRequest;
import com.cloudspace.jindun.network.base.RequestFactory;
import com.cloudspace.jindun.utils.ToastMaker;

/**
 * Created by zengxianhua on 16/12/31.
 */

public class OrigApi extends BaseAPI{
    public  void getOrig(Object tag, String id, ApiCallback<OrganizationListResult> apiCallback) {
        String url = String.format("http://%s/jindun/V1.0/phone/%s/Organ",Configs.SERVER_IP, id);
        GsonRequest request = RequestFactory.newInstance()
                .create(url, OrganizationListResult.class, apiCallback)
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

    public  void getOrigPersons(Object tag, String id, ApiCallback<ContactPersonListResult> apiCallback) {
        String url = String.format("http://%s/jindun/V1.0/phone/%s/linkPer", Configs.SERVER_IP, id);
        GsonRequest request = RequestFactory.newInstance()
                .create(url, ContactPersonListResult.class, apiCallback)
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

    public  void getContactPersons(Object tag, String userid, ApiCallback<ContactPersonListResult> apiCallback) {
        String url = String.format("http://%s/jindun/V1.0/phone/%s/isPer", Configs.SERVER_IP, userid);
        GsonRequest request = RequestFactory.newInstance()
                .create(url, ContactPersonListResult.class, apiCallback)
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

    public  void getEmergencyContactPersons(Object tag, String userid, ApiCallback<ContactPersonListResult> apiCallback) {
        String url = String.format("http://%s/jindun/V1.0/phone/%s/queryExiPer", Configs.SERVER_IP, userid);
        GsonRequest request = RequestFactory.newInstance()
                .create(url, ContactPersonListResult.class, apiCallback)
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

    public void remarkFriend(Object tag, String userId, String friLinkId, String remarkName, ApiCallback<RemarkResult> apiCallback) {
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(friLinkId) || TextUtils.isEmpty(remarkName)){
            ToastMaker.showToastLong(JindunApp.getInstance().getString(R.string.parameter_error));
            return;
        }

        String url = String.format("http://%s/jindun/V1.0/phone/remark", Configs.SERVER_IP);
        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, RemarkResult.class, apiCallback)
                .setParams("userId", userId)
                .setParams("friLinkId", friLinkId)
                .setParams("remarkName", remarkName)
                .setNoCache();
        HttpManager.addRequest(request, tag);

    }

    public void addFriend(Object tag, String userId, String friLinkId, ApiCallback<RemarkResult> apiCallback) {
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(friLinkId)){
            ToastMaker.showToastLong(JindunApp.getInstance().getString(R.string.parameter_error));
            return;
        }
        String url = String.format("http://%s/jindun/V1.0/phone/addLinkPer", Configs.SERVER_IP);
        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, RemarkResult.class, apiCallback)
                .setParams("userId", userId)
                .setParams("friLinkId", friLinkId)
                .setNoCache();
        HttpManager.addRequest(request, tag);

    }

    public void isExiPerson(Object tag, String userId, String friLinkId,boolean isExi,  ApiCallback<RemarkResult> apiCallback) {
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(friLinkId)){
            ToastMaker.showToastLong(JindunApp.getInstance().getString(R.string.parameter_error));
            return;
        }
        String url = String.format("http://%s/jindun/V1.0/phone/isExiPer", Configs.SERVER_IP);
        String isExiStr = (isExi) ? "0001" : "0002";
        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, RemarkResult.class, apiCallback)
                .setParams("userId", userId)
                .setParams("friendId", friLinkId)
                .setParams("isExi", isExiStr)
                .setNoCache();
        HttpManager.addRequest(request, tag);

    }


    public void deleteFriend(Object tag, String userId, String friLinkId, ApiCallback<RemarkResult> apiCallback) {
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(friLinkId)){
            ToastMaker.showToastLong(JindunApp.getInstance().getString(R.string.parameter_error));
            return;
        }
        String url = String.format("http://%s/jindun/V1.0/phone/delete", Configs.SERVER_IP);
        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, RemarkResult.class, apiCallback)
                .setParams("userId", userId)
                .setParams("friLinkId", friLinkId)
                .setNoCache();
        HttpManager.addRequest(request, tag);

    }
}
